package com.winai.launcher.manager;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.ks.projectbasictools.base.SimpleView;
import com.winai.launcher.R;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.override.FlikerProgressBar;
import com.winai.launcher.override.ToastKs;
import com.yhao.floatwindow.FloatActivity;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.Screen;

/**
 * 作者：康少
 * 时间：2020/8/28
 * 说明：升级进度弹窗
 */
public class UpgradeProgressManager {
    private Context mContext;
    private static UpgradeProgressManager mInstance;
    public static final String WINDOW_TAG = "upgrade_progress_window_tag";

    private AlphaAnimation showAnim, hideAnim;
    private Animation loadingAnimation;
    private SimpleView simpleView;
    private FlikerProgressBar flikerbar;
    private TextView tvTip;//底部tip
    private TextView tvStatus;//logo下面的tip
    private TextView tvVersion;//版本显示
    private TextView tvProgress;//进度显示
    private ImageView ivLoading;//加载中

    private String filePath = "";
    private String versionName = "";

    public static synchronized UpgradeProgressManager getInstance(Context mContext) {
        if (mInstance == null) {
            synchronized (UpgradeProgressManager.class) {
                if (null == mInstance) {
                    mInstance = new UpgradeProgressManager(mContext.getApplicationContext());
                }
            }
        }
        return mInstance;
    }

    private UpgradeProgressManager(Context mContext) {
        this.mContext = mContext;
        initTipView();

        showAnim = new AlphaAnimation(0, 1);
        showAnim.setDuration(500);
        hideAnim = new AlphaAnimation(1, 0);
        hideAnim.setDuration(500);

        loadingAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_loading);
        loadingAnimation.setInterpolator(new LinearInterpolator());
    }

    /**
     * @Desc 初始化提示视图
     */
    private void initTipView() {
        simpleView = new SimpleView(mContext, R.layout.dialog_upgrade_progress);
        tipViewListener(simpleView);

        FloatWindow
                .with(mContext)
                .setView(simpleView)
                .setTag(WINDOW_TAG)
                .setWidth(Screen.width, 1.0f) //设置悬浮控件宽高
                .setHeight(Screen.height, 1.0f)
                .setX(0)
                .setY(0)
                .setGravity(Gravity.BOTTOM | Gravity.START)
                .setDesktopShow(false) //桌面显示
                .setMoveType(MoveType.inactive, 0, 0)
                .setFilter(true, FloatActivity.class)/*这行代码必须要有，类名可以是任意类*/
                .build();
    }

    private void tipViewListener(View simpleView) {
        flikerbar = simpleView.findViewById(R.id.flikerbar);
        tvTip = simpleView.findViewById(R.id.tv_tip);
        tvStatus = simpleView.findViewById(R.id.tv_status);
        tvVersion = simpleView.findViewById(R.id.tv_version);
        ivLoading = simpleView.findViewById(R.id.iv_loading);
        tvProgress = simpleView.findViewById(R.id.tv_progress);
    }

    public synchronized void setVersionName(String version) {
        if (!TextUtils.isEmpty(version)) {
            versionName = version;
        }
    }

    public synchronized void setProgress(int num) {
        flikerbar.setProgress(num);
        tvProgress.setText(num + "%");
        if (num == 100) {
            flikerbar.finishLoad();
            tvTip.setText("下载完成，即将开始验证安装文件");
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    showComplete();
                }
            }, 2000);
        }
    }

    public synchronized void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * @date 2020/9/7
     * @desc 下载完成
     */
    private void showComplete() {
        ivLoading.setVisibility(View.VISIBLE);
        ivLoading.startAnimation(loadingAnimation);
        tvProgress.setVisibility(View.INVISIBLE);
        flikerbar.setVisibility(View.INVISIBLE);
        tvStatus.setText("验证安装文件");
        tvTip.setText("验证后会自动重启和更新");

        // 下载完成回调
        if (!TextUtils.isEmpty(filePath)) {
            GlobalPublisher.Notify(GlobalPublisher.DOWNLOAD_ROM_COMPLETE, filePath);
        } else {
            dismissTip();
            ToastKs.show(mContext, "文件已损坏!");
        }
    }

    /**
     * @Desc 展示下载
     */
    public void showTip() {
        if (FloatWindow.get(WINDOW_TAG) != null && !FloatWindow.get(WINDOW_TAG).isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    FloatWindow.get(WINDOW_TAG).show();
                }
            }, 500);
            simpleView.startAnimation(showAnim);
            //展示下载
            tvVersion.setText("设备版本:" + versionName);
            ivLoading.setVisibility(View.INVISIBLE);
            flikerbar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * @Desc 关闭引导提示蒙层
     */
    public void dismissTip() {
        if (FloatWindow.get(WINDOW_TAG) != null && FloatWindow.get(WINDOW_TAG).isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    FloatWindow.get(WINDOW_TAG).hide();
                    flikerbar.reset();
                }
            }, 500);
            simpleView.startAnimation(hideAnim);
        }
    }
}
